<?php
if(!defined('BASEPATH')) { exit('BASEPATH NOT SET'); }
/**
 * Template, handles all content and flushes it add the end of the script
 *
 * @author Merijn <mrbellek@gmail.com>
 * @author Dennis <d.enderink@gmail.com>
 */
class Template {

    /**
     * @var $html HTML to load
     */
    private $html               = '';
    /**
     * @var $basicVariables
     */
    private $basicVariables     = array();
    /**
     * @var $placeholderOpen
     */
    private $placeholderOpen    = '{';
    /**
     * @var $placeholderClose
     */
    private $placeholderClose   = '}';
    /**
     * @var $loadFooter
     */
    private $loadFooter         = TRUE;

    public function getContent($pageName, $replaces) {
        return str_replace(array_keys($replaces), array_values($replaces), file_get_contents('./view/' . $pageName . '.html'));
    }

    /**
     * Loads header and footer views
     *
     * @param string $file Which file to load? Header or footer
     *
     * @return self
     */
    public function loadBaseView($file) {
        $path           = BASEPATH . '/includes/' . $file . '.inc.html';

        if (is_readable($path)) {
            $this->html     .= file_get_contents($path);
        } else {
            //couldn't load view
        }

        return $this;
    }

    /**
     * Loads contents of view into $html
     *
     * @param string $pageName Name of file found in view folder
     * @param array $replaces Placeholders with corresponding replacements
     *
     * @return self
     */
    public function loadContent($pageName, $replaces = array()) {
        $path           = BASEPATH . '/view/' . $pageName . '.html';

        if (is_readable($path)) {
            //get contents of page
            $pageContents   = file_get_contents($path);

            //replace placeholders with private replaces
            $pageContents   = $this->replacePlaceholders($pageContents, $replaces);

            //set to html
            $this->html     .= $pageContents;
        } else {
            //couldn't load view
        }

        return $this;
    }

    /**
     * Replaces placeholders for their values
     *
     * @param string $html HTML to find and replace placeholders in
     * @param array $replaces
     *
     * @return string HTML with replaced placeholders
     */
    private function replacePlaceholders($html, $replaces) {
        return str_replace(array_map(array($this, 'addPlaceholderTags'), array_keys($replaces)), array_values($replaces), $html);
    }

    /**
     * Adds placeholder open and close tags if they aren't set already
     *
     * @param string $value Placeholder
     *
     * @return string Placeholder including tags
     */
    private function addPlaceholderTags($value) {
        //fallback for values with brackets already included
      if (preg_match('/\\' . $this->placeholderOpen . '(.*)\\' . $this->placeholderClose . '/', $value))
            return $value;

        return $this->placeholderOpen . $value . $this->placeholderClose;
    }

    /**
     * Whether to load footer or not (default TRUE)
     *
     * @param bool $load
     *
     * @return self
     */
    public function loadFooter($load) {
        $this->loadFooter                   = (bool) $load;

        return $this;
    }

    /**
     * Set data
     *
     * @param string $key Name of placeholder
     * @param string $value Value to which replace placeholder with
     *
     * @return self
     */
    public function __set($key, $value) {
        $this->basicVariables[$key]         = $value;

        return $this;
    }

    public function __destruct() {
        //load footer
        if ($this->loadFooter)
            $this->loadBaseView('footer');

        //replace basic variables
        $this->__set('timer_pageload', round(microtime(TRUE) - TIMER_START, 2) . 's');
        $this->html                         = $this->replacePlaceholders($this->html, $this->basicVariables);

        echo $this->html;
    }
}
?>
